import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { User } from './user/entities/user.entity';
import { Role } from './user/entities/role.entity';
import { Permission } from './user/entities/permission.entity';
import { RedisModule } from './redis/redis.module';
import { EmailModule } from './email/email.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { APP_GUARD } from '@nestjs/core';
import { LoginGuard } from './login.guard';
import { PermissionGuard } from './permission.guard';
import { EnterpriseModule } from './enterprise/enterprise.module';
import { Enterprise } from './enterprise/entities/enterprise.entity';
import { EnterpriseBackground } from './enterprise/entities/enterprise-background.entity';
import { Industry } from './enterprise/entities/industry.entity';
import { Patent } from './enterprise/entities/patent.entity';
import { Trademark } from './enterprise/entities/trademark.entity';
import { Executive } from './enterprise/entities/executive.entity';
import { Favorite } from './enterprise/entities/favorite.entity';
import { CommunityModule } from './community/community.module';
import { Post } from './community/entities/post.entity';
import { Comment } from './community/entities/comment.entity';
import { PostLike } from './community/entities/postLikes.entity';
import { CommentLikes } from './community/entities/commentLikes.entity';
import { StatisticModule } from './statistic/statistic.module';
import { UserLogin } from './user/entities/user-login.entity';
import { DailyCommentStats } from './statistic/entities/dailyCommentStats.entity';
import { EnterpriseSearchStats } from './statistic/entities/enterpriseSearchStats.entity';
import { TopKeywords } from './statistic/entities/topKeywords .entity';
import { DailyKeyStats } from './statistic/entities/dailyKeyStats.entity';
import { NotificationModule } from './notification/notification.module';
import { NotificationGateway } from './notification/notification.gateway';
import { Notification } from './notification/entities/notification.entity';
import { EnterpriseRisk } from './enterprise/entities/enterprise-risk.entity';
import { EnterpriseService } from './enterprise/enterprise.service';

@Module({
  imports: [
    NotificationModule,
    JwtModule.registerAsync({
      global: true,
      useFactory(configService: ConfigService) {
        return {
          secret: configService.get('jwt_secret'),
          signOptions: {
            expiresIn: '30m',
          },
        };
      },
      inject: [ConfigService],
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: 'src/.env',
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3307,
      username: 'root',
      password: 'liuhao',
      database: 'yichaqi_system',
      synchronize: true,
      timezone: 'Asia/Shanghai',
      logging: true,
      entities: [
        User,
        Role,
        Permission,
        Enterprise,
        EnterpriseBackground,
        Industry,
        Patent,
        Trademark,
        Executive,
        Favorite,
        Post,
        Comment,
        PostLike,
        CommentLikes,
        UserLogin,
        DailyCommentStats,
        EnterpriseSearchStats,
        TopKeywords,
        DailyKeyStats,
        Notification,
        EnterpriseRisk,
      ],
      poolSize: 10,
      connectorPackage: 'mysql2',
      extra: {
        authPlugin: 'sha256_password',
      },
    }),
    UserModule,
    RedisModule,
    EmailModule,
    EnterpriseModule,
    CommunityModule,
    StatisticModule,
    NotificationModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    NotificationGateway,
    {
      provide: APP_GUARD,
      useClass: LoginGuard,
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard,
    },
  ],
})
export class AppModule {}
